<!--
 * @Descripttion: 会员权益
 * @Author: xiao li
 * @Date: 2020-07-06 12:17:07
 * @LastEditors: DXV-RGWU-TUFH-RFCY-IEGMYY
 * @LastEditTime: 2023-08-08 18:31:24
-->
<template>
  <div>
    <top-nav></top-nav>
    <div class="page-main" v-if="!loading">
      <el-row class="page-top-operate flex-warp">
        <lb-button
          type="primary"
          @click="$router.push(`/custom/member/rights/edit`)"
          icon="el-icon-plus"
          v-hasPermi="`${$route.name}-add`"
          >{{ $t('menu.CustomMemberRightsAdd') }}</lb-button
        >
      </el-row>
      <el-table
        v-loading="loading"
        :data="tableData"
        :header-cell-style="{ background: '#f5f7fa', color: '#606266' }"
        style="width: 100%"
      >
        <el-table-column prop="title" label="权益名称"> </el-table-column>
        <el-table-column prop="show_title" label="展示名称"> </el-table-column>
        <el-table-column prop="rights_icon" label="权益图标">
          <template slot-scope="scope">
            <lb-image :src="scope.row.rights_icon"></lb-image>
          </template>
        </el-table-column>
        <el-table-column prop="type" label="服务方式">
          <template slot-scope="scope">系统核销</template>
        </el-table-column>
        <el-table-column prop="status_text" label="权益状态">
          <template slot-scope="scope">{{
            scope.row.status === 1 ? '使用中' : '已停用'
          }}</template>
        </el-table-column>
        <el-table-column prop="top" label="排序"> </el-table-column>
        <el-table-column prop="address" label="是否开启">
          <template slot-scope="scope">
            <el-switch
              v-model="scope.row.status"
              :active-value="1"
              :inactive-value="0"
              @change="updateItem(scope.row.id, scope.row.status)"
            >
            </el-switch>
          </template>
        </el-table-column>
        <el-table-column prop="status" label="操作">
          <template slot-scope="scope">
            <div class="table-operate">
              <lb-button
                type="primary"
                size="mini"
                plain
                @click="
                  $router.push(`/custom/member/rights/edit?id=${scope.row.id}`)
                "
                v-hasPermi="`${$route.name}-edit`"
                >{{ $t('action.edit') }}</lb-button
              >
            </div>
          </template>
        </el-table-column>
      </el-table>
      <lb-page
        :batch="false"
        :page="searchForm.page"
        :pageSize="searchForm.limit"
        :total="total"
        @handleSizeChange="handleSizeChange"
        @handleCurrentChange="handleCurrentChange"
      >
      </lb-page>
    </div>
  </div>
</template>

<script>
export default {
  data () {
    return {
      appPath: '',
      loading: false,
      tableData: [],
      searchForm: {
        page: 1,
        limit: 10
      },
      total: 0
    }
  },
  activated () {
    this.getTableDataList()
  },
  methods: {
    handleSizeChange (val) {
      this.searchForm.limit = val
      this.handleCurrentChange(1)
    },
    handleCurrentChange (val) {
      this.searchForm.page = val
      this.getTableDataList()
    },
    getTableDataList () {
      this.loading = true
      let { searchForm } = this
      this.$api.member.rightsList(searchForm).then(res => {
        this.loading = false
        if (res.code === 200) {
          this.tableData = res.data.data
          this.total = res.data.total
        }
      })
    },
    // 上下架、删除
    updateItem (id, status) {
      this.$api.member.rightsUpdate({ id, status }).then(res => {
        if (res.code === 200) {
          this.$message.success(this.$t(status === -1 ? 'tips.successDel' : 'tips.successOper'))
          if (status === -1) {
            this.searchForm.page = this.searchForm.page < Math.ceil((this.total - 1) / this.searchForm.limit) ? this.searchForm.page : Math.ceil((this.total - 1) / this.searchForm.limit)
            this.getTableDataList()
          }
        }
      })
    },
    // 删除
    confirmDel (id) {
      this.$confirm(this.$t('tips.confirmDelete'), this.$t('tips.reminder'), {
        confirmButtonText: this.$t('action.comfirm'),
        cancelButtonText: this.$t('action.cancel'),
        type: 'warning'
      }).then(() => {
        this.updateItem(id, -1)
      })
    }
  }
}
</script>

<style lang="scss" scoped>
</style>
